package com.shelpe.services.algorithm.entity.jpa;

import java.util.Date;

import javax.persistence.AttributeConverter;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import com.shelpe.services.algorithm.utils.EntityUtils;

import lombok.Data;

@Entity(name = "semiautotag")
@Data
public class Semiauto {
	public final static int DEVICE_MOBILE = 0;
	public final static int DEVICE_PC = 1;
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name = "id")
	private long autoID;
	@Column(name = "user_id")
	private long userID;
	private String title;
	private String content;
	
	@Column(name = "add_time")
	@Convert(converter = EntityUtils.Day2IntConverter.class)
	private Date createTime;
	
	@Convert(converter = TypeConverter.class)
	private Type type;
	@Column(name = "adgroup_id")
	private String adgroupID;
	@Column(name = "campaign_id")
	private String campaignID;
	private String typeValue;
	
	@Convert(converter = StatusConverter.class)
	private Status status;
	@Convert(converter = PlatformConverter.class)
	private Platform platform;
	@Column(name = "data_source")
	private int device;
	
	public static enum Platform {
		special(0), platform(1), platformCopy(2);
		private int value;

		private Platform(int v) {
			this.value = v;
		}

		public int getValue() {
			return this.value;
		}
	}
	
	public static class PlatformConverter implements AttributeConverter<Platform, Integer> {

		@Override
		public Integer convertToDatabaseColumn(Platform attribute) {
			return attribute != null ? attribute.getValue() : Platform.special.getValue();
		}

		@Override
		public Platform convertToEntityAttribute(Integer dbData) {
			Platform status = Platform.special;
			for (Platform s : Platform.values()) {
				if (s.getValue() == dbData) {
					status = s;
					break;
				}
			}
			return status;
		}
	}
	
	
	public static enum Status {
		normal(1), abnormal(2);
		private int value;

		private Status(int v) {
			this.value = v;
		}

		public int getValue() {
			return this.value;
		}
	}
	
	public static class StatusConverter implements AttributeConverter<Status, Integer> {

		@Override
		public Integer convertToDatabaseColumn(Status attribute) {
			return attribute != null ? attribute.getValue() : Status.normal.getValue();
		}

		@Override
		public Status convertToEntityAttribute(Integer dbData) {
			Status status = Status.normal;
			for (Status s : Status.values()) {
				if (s.getValue() == dbData) {
					status = s;
					break;
				}
			}
			return status;
		}
	}
	
	public static enum Type{
		account(1), campaign(2), product(3), batch(4);
		private int value;

		private Type(int v) {
			this.value = v;
		}

		public int getValue() {
			return this.value;
		}
		
		public static Type to(String name){
			for(Type type:Type.values()){
				if(type.name().equals(name)){
					return type;
				}
			}
			return Type.batch;
		}
	}
	
	public static class TypeConverter implements AttributeConverter<Type, Integer> {

		@Override
		public Integer convertToDatabaseColumn(Type attribute) {
			return attribute != null ? attribute.getValue() : Type.account.getValue();
		}

		@Override
		public Type convertToEntityAttribute(Integer dbData) {
			Type status = Type.account;
			for (Type s : Type.values()) {
				if (s.getValue() == dbData) {
					status = s;
					break;
				}
			}
			return status;
		}
	}
}
